﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MagicKey.Components.Database;
using MagicKey.Factory;
using MagicKey.Interfaces;

namespace MagicKey.UserInterface
{
    /// <summary>
    /// Interaktionslogik für PasswordViewer.xaml
    /// </summary>
    public partial class PasswordViewer : Window
    {
        public PasswordViewer(Fileviewer fileviewer)
        {
            InitializeComponent();
            this.Owner = fileviewer;
        }

        private void Valided_Click(object sender, RoutedEventArgs e)
        {
            // get pass != string.empty
            if (!this.PasswordTextBox.Password.Equals(string.Empty))
            {
                IMagicKey magicKey = MagicKeyFactory.GetInstance().GetMagicKeyInterface();
                var accessManager = magicKey.GetAccessManager();
                var fileAccess = magicKey.GetFileAccess();
                IDatabase db = new MagicDatabase();

                var hashedPassword = accessManager.HashPassword(this.PasswordTextBox.Password);

                var user = ((Fileviewer)this.Owner).MasterUser;

                var salt = db.LoadParameterUser("salt", user.Name.ToLower());
                var symetricKey = db.LoadParameterUser("symetricKey", user.Name.ToLower());
                string path = ((Fileviewer)this.Owner).SelectedFile;

                // Hash a hashed password with salt
                hashedPassword = accessManager.HashHashedPasswordAndSalt(hashedPassword, salt);

                // Load saved password
                var savedPassword = db.LoadParameterUser("password", user.Name.ToLower());

                if (savedPassword != null && hashedPassword.Equals(savedPassword))
                {
                    var isFileLoaded = fileAccess.LoadFile(user.Id, user.Name, path);

                    if (!isFileLoaded)
                    {
                        MessageBox.Show("You cannot access this file!", "Fileviewer",
                            MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    }
                    else
                    {

                        string filename = path.Substring(path.LastIndexOf('/') + 1);
                        string DirectoryPath = "C:/magic_key/";
                        string decryptedASym = accessManager.DecryptFileAsym(user.Name.ToLower(), PasswordTextBox.Password,
                            DirectoryPath + filename);
                        // Destroy a password
                        this.PasswordTextBox.Password = null;
                        string decryptedSym = accessManager.DecryptFileSym(decryptedASym, symetricKey);
                        Process.Start(decryptedSym);
                        ((Fileviewer)this.Owner).IsSelectedFileDecrypt = true;

                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("Your pawword is wrong", "PasswordViewer",
                       MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }

            }

        }

    }
}
